﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.WindowsAzure.StorageClient
Imports Microsoft.Samples.WindowsPhoneCloud.Web.UserAccountWrappers
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Models
Imports Microsoft.Samples.WindowsPhoneCloud.Web.Infrastructure
Imports System.Globalization
Imports System.Collections.Generic

Namespace Controllers

    Public Class QueuesController
        Inherits StorageItemController
        Private ReadOnly cloudQueueClient As CloudQueueClient

#If ACS Then
        Private ReadOnly userRepository As IUserRepository

        Public Sub New()
            Me.New(Nothing, New UserTablesServiceContext(), New UserTablesServiceContext())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal cloudQueueClient As CloudQueueClient, ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal userRepository As IUserRepository)
            MyBase.New(userPrivilegesRepository)
            If (GetStorageAccountFromConfigurationSetting() Is Nothing) AndAlso (cloudQueueClient Is Nothing) Then
                Throw New ArgumentNullException("cloudQueueClient", "Cloud Queue Client cannot be null if no configuration is loaded.")
            End If

            Me.cloudQueueClient = If(cloudQueueClient, GetStorageAccountFromConfigurationSetting().CreateCloudQueueClient())
            Me.userRepository = userRepository
        End Sub

        Public Function Index() As ActionResult
            Dim permissions = New List(Of StorageItemPermissionsModel)()
            Dim queues = Me.cloudQueueClient.ListQueues().Select(Function(q) q.Name)
            For Each queueName In queues
                Dim accessQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queueName, PrivilegeConstants.QueuePrivilegeSuffix)
                Dim publicQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queueName, PrivilegeConstants.PublicQueuePrivilegeSuffix)
                permissions.Add(New StorageItemPermissionsModel With {.StorageItemName = queueName, .IsPublic = Me.UserPrivilegesRepository.PublicPrivilegeExists(publicQueuePrivilege), .AllowedUserIds = Me.UserPrivilegesRepository.GetUsersWithPrivilege(accessQueuePrivilege).Select(Function(us) us.UserId)})
            Next queueName

		   Me.ViewData("users") = Me.userRepository.GetAllUsers().Select(Function(user) New UserModel With {.UserName = user.Name, .UserId = user.UserId})

            Return Me.View(permissions)
        End Function
#Else
        Private ReadOnly membershipService As IMembershipService

        Public Sub New()
            Me.New(Nothing, New UserTablesServiceContext(), New AccountMembershipService())
        End Sub

        <CLSCompliant(False)> _
        Public Sub New(ByVal cloudQueueClient As CloudQueueClient, ByVal userPrivilegesRepository As IUserPrivilegesRepository, ByVal membershipService As IMembershipService)
            MyBase.New(userPrivilegesRepository)
            If (GetStorageAccountFromConfigurationSetting() Is Nothing) AndAlso (cloudQueueClient Is Nothing) Then
                Throw New ArgumentNullException("cloudQueueClient", "Cloud Queue Client cannot be null if no configuration is loaded.")
            End If

            Me.cloudQueueClient = If(cloudQueueClient, GetStorageAccountFromConfigurationSetting().CreateCloudQueueClient())
            Me.membershipService = membershipService
        End Sub

        Public Function Index() As ActionResult
            Dim permissions = New List(Of StorageItemPermissionsModel)()
            Dim queues = Me.cloudQueueClient.ListQueues().Select(Function(q) q.Name)
            For Each queueName In queues
                Dim accessQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queueName, PrivilegeConstants.QueuePrivilegeSuffix)
                Dim publicQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queueName, PrivilegeConstants.PublicQueuePrivilegeSuffix)
                permissions.Add(New StorageItemPermissionsModel With {.StorageItemName = queueName, .IsPublic = Me.UserPrivilegesRepository.PublicPrivilegeExists(publicQueuePrivilege), .AllowedUserIds = Me.UserPrivilegesRepository.GetUsersWithPrivilege(accessQueuePrivilege).Select(Function(us) us.UserId)})
            Next queueName

            Me.ViewData("users") = Me.membershipService.GetAllUsers().Cast(Of MembershipUser)().Select(Function(user) New UserModel With {.UserName = user.UserName, .UserId = user.ProviderUserKey.ToString()})

            Return Me.View(permissions)
        End Function
#End If

        <HttpPost()> _
        Public Sub AddQueuePermission(ByVal queue As String, ByVal userId As String)
            Dim accessQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queue, PrivilegeConstants.QueuePrivilegeSuffix)
            Me.AddPrivilegeToUser(userId, accessQueuePrivilege)
        End Sub

        <HttpPost()> _
        Public Sub RemoveQueuePermission(ByVal queue As String, ByVal userId As String)
            Dim accessQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queue, PrivilegeConstants.QueuePrivilegeSuffix)
            Me.RemovePrivilegeFromUser(userId, accessQueuePrivilege)
        End Sub

        <HttpPost()> _
        Public Sub SetQueuePublic(ByVal queue As String, ByVal isPublic As Boolean)
            Dim accessQueuePrivilege = String.Format(CultureInfo.InvariantCulture, "{0}{1}", queue, PrivilegeConstants.PublicQueuePrivilegeSuffix)
            Me.SetPublicPrivilege(accessQueuePrivilege, isPublic)
        End Sub
    End Class
End Namespace